package cn.cq.admin.service.impl;

import cn.cq.admin.entity.dto.CoursewareDto;
import cn.cq.admin.entity.model.Course;
import cn.cq.admin.entity.vo.CourseVo;
import cn.cq.admin.entity.vo.CoursewareVo;
import cn.cq.admin.entity.vo.TreeVo;
import cn.cq.admin.mapper.CourseMapper;
import cn.cq.admin.service.CourseService;
import cn.cq.admin.service.UploadService;
import cn.cq.admin.service.UserCourseCoursewareService;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cq.admin.entity.model.Courseware;
import cn.cq.admin.service.CoursewareService;
import cn.cq.admin.mapper.CoursewareMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author  dicraft
* @description 针对表【courseware】的数据库操作Service实现
* @createDate 2022-10-03 14:30:44
*/
@Service
@Slf4j
public class CoursewareServiceImpl extends ServiceImpl<CoursewareMapper, Courseware>
    implements CoursewareService{

    @Resource
    private CourseMapper courseMapper;
    @Resource
    private CourseService courseService;
    @Resource
    private UploadService uploadService;
    @Resource
    private UserCourseCoursewareService uccService;

    @Override
    public Page<CoursewareVo> doList(CoursewareDto coursewareDto) {
        Page<Courseware> coursePage = new Page<>(coursewareDto.getPageNo(),coursewareDto.getPageSize());
        List<CoursewareVo> collect = lambdaQuery()
                .like(StrUtil.isNotBlank(coursewareDto.getCoursewareName()), Courseware::getCoursewareName, coursewareDto.getCoursewareName())
                .eq(ObjectUtil.isNotNull(coursewareDto.getCourseId()), Courseware::getCourseId, coursewareDto.getCourseId())
                .page(coursePage)
                .getRecords()
                .stream()
                .map(courseware -> {
                    CoursewareVo coursewareVo = new CoursewareVo();
                    coursewareVo.setId(String.valueOf(courseware.getId()));
                    coursewareVo.setCoursewareName(courseware.getCoursewareName());
                    coursewareVo.setCourseId(String.valueOf(courseware.getCourseId()));
                    String videoId = courseware.getVideoId();
                    //根据videoId获取视频地址
                    String address = uploadService.getVideoAddress(videoId);
                    coursewareVo.setAddress(address);
                    coursewareVo.setVideoId(courseware.getVideoId());
                    //根据id查询course对象
                    Course course = courseService.getById(courseware.getCourseId());
                    coursewareVo.setCourseName(course.getCourseName());
                    return coursewareVo;
                })
                .collect(Collectors.toList());
        Page<CoursewareVo> coursewareVoPage = new Page<>();
        coursewareVoPage.setRecords(collect);
        coursewareVoPage.setTotal(coursePage.getTotal());
        coursewareVoPage.setSize(coursePage.getSize());
        coursewareVoPage.setCurrent(coursePage.getCurrent());
        return coursewareVoPage;
    }

    @Override
    public void doSave(Courseware courseware) {
        log.info("coursewareDto:{}", courseware);
        save(courseware);
    }

    @Override
    public void doRemove(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void doModify(Courseware courseware) {
        updateById(courseware);

    }

    @Override
    public List<TreeVo> doListCourseAndWareByUserId() {
        Long userId = StpUtil.getLoginIdAsLong();
        //通过用户id查找对应的批次
        List<Course> courses = courseMapper.listAllByUserId(userId);
        //通过课程查找课件
        List<TreeVo> treeVos = courses.stream().map(course -> {
            TreeVo treeVo = new TreeVo();
            treeVo.setId(String.valueOf(course.getId()));
            treeVo.setLabel(course.getCourseName());
            //通过课程id查找课件
            List<TreeVo> children = this
                    .lambdaQuery()
                    .eq(Courseware::getCourseId, course.getId())
                    .list()
                    .stream()
                    .map(courseware -> {
                        //通过userId，courseId，coursewareId查找笔记
                        log.info("userId:{},courseId:{},coursewareId:{}",userId,course.getId(),courseware.getId());
                        String notes = uccService.getNotes(course.getId(), courseware.getId());
                        TreeVo childrenTree = new TreeVo();
                        childrenTree.setId(String.valueOf(courseware.getId()));
                        childrenTree.setLabel(courseware.getCoursewareName());
                        //获取地址
                        String videoId = courseware.getVideoId();
                        childrenTree.setNotes(notes);
                        String address = uploadService.getVideoAddress(videoId);
                        childrenTree.setAddress(address);
                        return childrenTree;
                    })
                    .collect(Collectors.toList());
            treeVo.setChildren(children);
            return treeVo;
        }).collect(Collectors.toList());
        return treeVos;
    }
}




